#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include <stdlib.h>

//int main()
//{
//  gid_t id =fork();
//
//  if(id == 0)
//  {
//    int cnt = 5;
//    //child
//    while(cnt)
//    {
//      printf("i am child process, mypid:%d, 退出还有%dS\n", getpid(), cnt--);
//      sleep(1);
//    }
//    exit(77);
//  }
//
//  //parent
//  int status = 0;
//  gid_t ret = waitpid(id, &status, 0);
//
//  printf("i an parent, child:%d, exit signal:%d, exit code:%d, coredump:%d\n", ret, status&0x7F, WEXITSTATUS(status), (status>>7)&1);
//
//  return 0;
//}
//

int main()
{
  gid_t id = fork();

  if(id == 0)
  {
    //子进程发生程序替换，使用的接口函数为execlp
    printf("i am chiild ,mypid:%d\n", getpid());
    execlp("ls", "ls", "-l", "-a", NULL);//最后以NULL结尾

    //后面的代码不会被执行
    printf("you can see me!\n");
    printf("you can see me!\n");
    printf("you can see me!\n");
    printf("you can see me!\n");
  }
  sleep(3);
  //parent
  int status = 0;
  gid_t ret = waitpid(id, &status, 0);
  printf("i am parent, child:%d\n", ret);
  return 0;
}
